﻿using gptDataType.Models;
using gptDataType.Type.combination;
using gptDataType.Type.integer;
using gptDataType.Type.bit;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gptDataType.Type.character;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using gptDataType.Type.floating;
using gptDataType.Type.time;
using System.Reflection;
using System.ComponentModel;

namespace gptDataType.Helper
{
    /// <summary>
    /// 表示数组声明的解析结果
    /// </summary>
    public class ArrayInfo
    {
        public string ArrayName { get; set; } = string.Empty;  // 数组名称
        public int Length { get; set; }        // 数组长度
        public string ElementType { get; set; } = string.Empty; // 元素类型

        /// <summary>
        /// 解析数组声明并返回相关信息
        /// </summary>
        /// <param name="input">输入的数组声明字符串</param>
        /// <returns>包含数组名称、长度和元素类型的对象</returns>
        public static ArrayInfo ParseArrayDeclaration(string input)
        {
            // 定义正则表达式
            //string pattern = @"(\w+)$(\d+)\.\.(\d+)$\s+of\s+(.+)";
            string pattern = @"Array\[(\d+)\.\.(\d+)\]\s*of\s*(""[^""]+""|\w+)";
            Regex regex = new Regex(pattern);

            // 匹配输入字符串
            Match match = regex.Match(input);
            if (!match.Success)
            {
                throw new ArgumentException("Invalid array declaration format.");
            }

            // 提取匹配的分组
            string arrayName = "Array";         // 数组名称
            int lowerBound = int.Parse(match.Groups[1].Value); // 下界
            int upperBound = int.Parse(match.Groups[2].Value); // 上界
            string elementType = match.Groups[3].Value.Trim('"'); // 元素类型（去除双引号）

            // 计算数组长度
            int length = upperBound - lowerBound + 1;

            // 返回结果
            return new ArrayInfo
            {
                ArrayName = arrayName,
                Length = length,
                ElementType = elementType
            };
        }
        /// <summary>
        /// 从字符串中提取方括号中的数字
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <returns>方括号中的数字</returns>
        public static int ExtractNumberFromBracket(string input)
        {
            // 定义正则表达式
            string pattern = @"String\[(\d+)\]";
            Regex regex = new Regex(pattern);

            // 匹配输入字符串
            Match match = regex.Match(input);
            if (match.Success)
            {
                // 提取匹配的数字并转换为整数
                return int.Parse(match.Groups[1].Value);
            }
            else
            {
                throw new ArgumentException("Invalid input format. Expected a string in the format 'string[<number>]'.");
            }
        }
    }
    #region 解析json
    public class CompareJson
    {
        public string folderPath { get; set; } = string.Empty;

        public List<string> jsonFiles { get; set; } = new List<string>();

        /// <summary>
        /// 用户自定义类型
        /// </summary>
        public readonly Dictionary<string, PlcStruct> customDefineType = new Dictionary<string, PlcStruct>();

        public readonly Dictionary<string, PlcVariableTable> allVariabletable = new Dictionary<string, PlcVariableTable>();

        /// <summary>
        /// 获取所有json文件
        /// </summary>
        /// <param name="folderPath">整体工程文件</param>

        public CompareJson(string folderPath)
        {
            this.folderPath = folderPath;
            jsonFiles = GetJsonFiles();
        }
        /// <summary>
        /// 获取指定文件夹下的所有 .json 文件的全路径列表
        /// </summary>
        /// <returns>包含所有 .json 文件全路径的列表</returns>
        public List<string> GetJsonFiles()
        {
            // 检查文件夹是否存在
            if (!Directory.Exists(folderPath))
            {
                throw new DirectoryNotFoundException($"文件夹路径不存在：{folderPath}");
            }

            // 获取文件夹下所有 .json 文件的全路径
            string[] jsonFiles = Directory.GetFiles(folderPath, "*.json", SearchOption.AllDirectories);

            // 将结果转换为 List<string> 并返回
            return new List<string>(jsonFiles);
        }
        public void ReadJsonFiles()
        {
            foreach (var file in jsonFiles)
            {
                // 读取 JSON 文件内容
                string jsonContent = File.ReadAllText(file);
                // 解析 JSON 字符串为 JObject
                JObject jsonObject = JObject.Parse(jsonContent);
                string fileType = (string)jsonObject["FileType"];
                switch (fileType)
                {
                    case "PlcStruct":
                        {
                            Debug.WriteLine("PlcStruct", file);
                            JObject interfaceObj = (JObject)jsonObject["Interface"];
                            string name = (string)jsonObject["Name"];
                            PlcStruct plcStruct = new PlcStruct(name);
                            customDefineType[name] = plcStruct;
                            compareInterface(interfaceObj, plcStruct);
                        }
                        
                        break;
                    case "FC":
                    case "FB":
                        {
                            Debug.WriteLine("FB/FC", file);
                            JObject interfaceObj = (JObject)jsonObject["Interface"];
                            string name = (string)jsonObject["Name"];
                            PlcStruct plcStruct = new PlcStruct(name);
                            customDefineType[name] = plcStruct;
                            compareInterface(interfaceObj, plcStruct);
                            PlcVariableTable plcVariableTable = new PlcVariableTable(name,DataType.Struct,0);
                            plcVariableTable.AddMember(plcStruct);
                            allVariabletable[name] = plcVariableTable;
                        }
                        break;
                    case "GlobalDB":
                    case "InstanceDB":
                        {
                            Debug.WriteLine("GlobalDB/InstanceDB", file);
                            JObject interfaceObj = (JObject)jsonObject["Interface"];
                            string name = (string)jsonObject["Name"];
                            PlcStruct plcStruct = new PlcStruct(name);
                            compareInterface(interfaceObj, plcStruct);
                            PlcVariableTable plcVariableTable = new PlcVariableTable(name, DataType.Struct, 0);
                            plcVariableTable.AddMember(plcStruct);
                            allVariabletable[name] = plcVariableTable;
                        }
                        break;
                    case "OB":
                        {
                            Debug.WriteLine("OB", file);
                            JObject interfaceObj = (JObject)jsonObject["Interface"];
                            string name = (string)jsonObject["Name"];
                            PlcStruct plcStruct = new PlcStruct(name);
                            compareInterface(interfaceObj, plcStruct);
                            PlcVariableTable plcVariableTable = new PlcVariableTable(name, DataType.Struct, 0);
                            plcVariableTable.AddMember(plcStruct);
                            allVariabletable[name] = plcVariableTable;
                        }
                        break;
                    default:
                        break;
                }
                //Debug.WriteLine(jsonObject.ToString());
            }
        }
        private void compareInterface(JObject jsonObject, PlcStruct plcVariableTable)
        {
            // 解析 JSON 字符串为 JObject
            JArray sections = (JArray)jsonObject["section"];
            foreach (var section in sections)
            {
                compareSection((JObject)section, plcVariableTable);
            }
        }
        /// <summary>
        /// 解析Menber:struct的数据在menber中
        /// </summary>
        /// <param name="jsonObject"></param>
        /// <param name="plcVariableTable"></param>
        private void compareMember(JObject jsonObject, PlcStruct plcstruct)
        {
            // 解析 JSON 字符串为 JObject

            // 解析每个 member 对象
            JObject memberObj = (JObject)jsonObject;
            string name = (string)memberObj["name"];
            string datatype = (string)memberObj["datatype"];
            switch (datatype)
            {
                //处理基本数据类型
                case "Int":
                    plcstruct.AddMember(new PlcInt(name, 0));
                    break;
                case "Bool":
                    plcstruct.AddMember(new PlcBool(name, false));
                    break;
                case "USInt":
                    plcstruct.AddMember(new PlcUSInt(name, 0));
                    break;
                case "SInt":
                    plcstruct.AddMember(new PlcSInt(name, 0));
                    break;
                case "DTL":
                    break;
                case "Time":
                    break;
                case "IEC_TIMER":
                    break;
                case "DWord":
                    plcstruct.AddMember(new PlcDWord(name, 0));
                    break;
                case "String":
                    plcstruct.AddMember(new PlcArray(name, new PlcChar(name, '0'), 256));
                    break;
                case "Struct":
                    // 处理结构体类型
                    var subStrcut = new PlcStruct(name);
                    // 递归解析嵌套的 struct
                    //compareMember((JObject)memberObj, plcstruct);
                    JArray subMembers = (JArray)memberObj["member"];
                    if (subMembers.Count > 0)
                    {
                        foreach (var subMember in subMembers)
                        {
                            compareMember((JObject)subMember, subStrcut);
                        }
                    }
                    plcstruct.AddMember(subStrcut);
                    break;
                default:
                    //处理Array开始的数组
                    if (datatype.StartsWith("Array["))
                    {
                        // 处理数组类型
                        // 解析数组声明
                        
                        ArrayInfo arrayInfo = ArrayInfo.ParseArrayDeclaration(datatype);
                        //根据不同的类型创建不同的数组
                        switch (arrayInfo.ElementType)
                        {
                            //处理基本数据类型
                            case "Int":
                                plcstruct.AddMember(new PlcArray(name,new PlcInt(name,0),arrayInfo.Length ));
                                break;
                            case "Bool":
                                plcstruct.AddMember(new PlcArray(name, new PlcBool(name, false), arrayInfo.Length));
                                break;
                            case "USInt":
                                plcstruct.AddMember(new PlcArray(name, new PlcUSInt(name, 0), arrayInfo.Length));
                                break;
                            case "SInt":
                                plcstruct.AddMember(new PlcArray(name, new PlcSInt(name, 0), arrayInfo.Length));
                                break;
                            case "DTL":
                                break;
                            case "Time":
                                break;
                            case "IEC_TIMER":
                                break;
                            case "DWord":
                                plcstruct.AddMember(new PlcArray(name, new PlcDWord(name, 0), arrayInfo.Length));
                                break;
                            default:
                                plcstruct.AddMember(new PlcArray(name, new PlcStruct(name, arrayInfo.ElementType), arrayInfo.Length));
                                break;
                        }
                        
                        
                    }
                    else if (datatype.StartsWith("String["))
                    {
                        int length = ArrayInfo.ExtractNumberFromBracket(datatype);
                        plcstruct.AddMember(new PlcArray(name, new PlcChar(name, '0'), length));
                    }
                    else
                    {
                        plcstruct.AddMember(new PlcStruct(name, datatype));
                        Debug.WriteLine($"不支持的数据类型: {datatype}，请检查数据类型定义。");
                    }
                    break;
            }
            Debug.WriteLine($"name={name} datatype={datatype}");

        }
        /// <summary>
        /// 用户自定义类型在section中
        /// </summary>
        /// <param name="jsonObject"></param>
        /// <param name="plcStruct"></param>
        private void compareSection(JObject jsonObject, PlcStruct plcStruct)
        {
            // 解析每个 section 对象
            JObject sectionObj = jsonObject;
            JArray members = (JArray)sectionObj["member"];
            string sectionName = (string)sectionObj["name"];
            //处理plsStruct
            if (sectionName == "None")
            {
                foreach (var member in members)
                {
                    // 解析每个 member 对象
                    compareMember((JObject)member, plcStruct);
                }
            }
            //非PlsStruct
            else
            {
                var sectionStrcut = new PlcStruct(sectionName);
                foreach (var member in members)
                {
                    // 解析每个 member 对象
                    compareMember((JObject)member, sectionStrcut);
                }
                plcStruct.AddMember(sectionStrcut);
            }

        }
    }
    #endregion
    /// <summary>
    /// 解析西门子提取的数据结构放到项目数据结构中
    /// </summary>
    public class ParseXmz2SelfDataStruct
    {
        
        /// <summary>
        /// 统计所用到的指令
        /// </summary>
        public readonly HashSet<string> allCammand=new HashSet<string>();
        public string FileDir { get; set; }
        public ParseXmz2SelfDataStruct(string FileDir)
        {
            this.FileDir = FileDir;
        }
        public void StartParse()
        {
            ParseXmzXml parseXmzXml = new(FileDir);
            parseXmzXml.ParseXmlFiles();
            List<FileInfo> fileInfos = parseXmzXml.fileInfos;
            foreach (FileInfo fileInfo in fileInfos)
            {
                Debug.WriteLine($"文件名：{fileInfo.FileName}");
                PlcStruct plcStructAll =new PlcStruct(fileInfo.FileName);
                ParseSection(fileInfo.Sections_T, plcStructAll);
                ParseFlgNetList(fileInfo.LadList);
                fileInfo.PlcStruct = plcStructAll;
                this.fileInfos.Add(fileInfo);
            }
        }
        /// <summary>
        /// 解析Sections，每个section是一个结构体
        /// </summary>
        /// <param name="sections_T"></param>
        /// <param name="plcStruct"></param>
        private void ParseSection(Sections_T sections_T, PlcStruct plcStruct)
        {
            if (sections_T.Section.Length > 0)
            {
                foreach (var sectionItem in sections_T.Section)
                {
                    
                    if(sectionItem.Member!=null&&sectionItem.Member.Length>0) {
                        string sectionName = Enum.GetName(typeof(SectionName_TE),sectionItem.Name)??string.Empty;
                        PlcStruct sectionStruct = new PlcStruct(sectionName);
                        if (sectionName!=null&& sectionName != string.Empty&&sectionItem.Member.Length > 0)
                        {
                            foreach (var memberItem in sectionItem.Member)
                            {
                                ParseMenber(memberItem, sectionStruct);
                            }
                        }
                        plcStruct.AddMember(sectionStruct);
                    }
                    
                    //Debug.WriteLine(sectionItem);
                }
            }
        }
        /// <summary>
        /// 解析Member,如果是标准struct,则进行递归解析
        /// </summary>
        /// <param name="member_T"></param>
        /// <param name="sectionStruct"></param>
        private void ParseMenber(Member_T member_T,PlcStruct sectionStruct)
        {
            string dataType = member_T.Datatype;
            string name = member_T.Name;
            switch (dataType)
            {
                //处理基本数据类型
                //位数据类型
                case "Bool":
                    sectionStruct.AddMember(new PlcBool(name, false));
                    break;
                case "Byte":
                    sectionStruct.AddMember(new PlcByte(name, 0));
                    break;
                case "Word":
                    sectionStruct.AddMember(new PlcWord(name, 0));
                    break;
                case "DWord":
                    sectionStruct.AddMember(new PlcDWord(name, 0));
                    break;
                //整数数据类型
                case "USInt":
                    sectionStruct.AddMember(new PlcUSInt(name, 0));
                    break;
                case "SInt":
                    sectionStruct.AddMember(new PlcSInt(name, 0));
                    break;
                case "UInt":
                    sectionStruct.AddMember(new PlcUInt(name, 0));
                    break;
                case "Int":
                    sectionStruct.AddMember(new PlcInt(name, 0));
                    break;
                case "UDInt":
                    sectionStruct.AddMember(new PlcUDInt(name, 0));
                    break;
                case "DInt":
                    sectionStruct.AddMember(new PlcDInt(name, 0));
                    break;
                //浮点数据类型
                case "Real":
                    sectionStruct.AddMember(new PlcReal(name, 0));
                    break;
                case "LReal":
                    sectionStruct.AddMember(new PlcLReal(name, 0));
                    break;
                //时间数据类型
                case "Time":
                    sectionStruct.AddMember(new PlcTime(name, 0));
                    break;
                case "Date":
                    sectionStruct.AddMember(new PlcDate(name, new DateTime()));
                    break;
                case "TOD":
                    sectionStruct.AddMember(new PlcToD(name, new TimeSpan()));
                    break;
                case "DTL":
                    sectionStruct.AddMember(new PlcDTL(name, new DateTimeOffset()));
                    break;
                //字符串数据类型
                case "Char":
                    sectionStruct.AddMember(new PlcChar(name, '0'));
                    break;
                case "WChar":
                    sectionStruct.AddMember(new PlcWChar(name, '0'));
                    break;
                case "String":
                    sectionStruct.AddMember(new PlcArray(name, new PlcChar(name, '0'), 256));
                    break;
                case "WString":
                    //sectionStruct.AddMember(new PlcChar(name, '0'));
                    break;
                case "IEC_TIMER":
                    break;
                
                case "Struct":
                    // 处理结构体类型
                    var subStrcut = new PlcStruct(name);
                    if (member_T.Items != null)
                    {
                        foreach (var item in member_T.Items)
                        {
                            if(item != null && item is Member_T)
                            {
                                ParseMenber(item as Member_T, subStrcut);
                            }
                        }
                    }
                    sectionStruct.AddMember(subStrcut);
                    break;
                default:
                    //处理Array开始的数组
                    if (dataType.StartsWith("Array["))
                    {
                        // 处理数组类型
                        // 解析数组声明

                        ArrayInfo arrayInfo = ArrayInfo.ParseArrayDeclaration(dataType);
                        //根据不同的类型创建不同的数组
                        switch (arrayInfo.ElementType)
                        {
                            //处理基本数据类型
                            //位数据类型
                            case "Bool":
                                sectionStruct.AddMember(new PlcBool(name, false));
                                break;
                            case "Byte":
                                sectionStruct.AddMember(new PlcByte(name, 0));
                                break;
                            case "Word":
                                sectionStruct.AddMember(new PlcWord(name, 0));
                                break;
                            case "DWord":
                                sectionStruct.AddMember(new PlcDWord(name, 0));
                                break;
                            //整数数据类型
                            case "USInt":
                                sectionStruct.AddMember(new PlcUSInt(name, 0));
                                break;
                            case "SInt":
                                sectionStruct.AddMember(new PlcSInt(name, 0));
                                break;
                            case "UInt":
                                sectionStruct.AddMember(new PlcUInt(name, 0));
                                break;
                            case "Int":
                                sectionStruct.AddMember(new PlcInt(name, 0));
                                break;
                            case "UDInt":
                                sectionStruct.AddMember(new PlcUDInt(name, 0));
                                break;
                            case "DInt":
                                sectionStruct.AddMember(new PlcDInt(name, 0));
                                break;
                            //浮点数据类型
                            case "Real":
                                sectionStruct.AddMember(new PlcReal(name, 0));
                                break;
                            case "LReal":
                                sectionStruct.AddMember(new PlcLReal(name, 0));
                                break;
                            //时间数据类型
                            case "Time":
                                sectionStruct.AddMember(new PlcTime(name, 0));
                                break;
                            case "Date":
                                sectionStruct.AddMember(new PlcDate(name, new DateTime()));
                                break;
                            case "TOD":
                                sectionStruct.AddMember(new PlcToD(name, new TimeSpan()));
                                break;
                            case "DTL":
                                sectionStruct.AddMember(new PlcDTL(name, new DateTimeOffset()));
                                break;
                            //字符串数据类型
                            case "Char":
                                sectionStruct.AddMember(new PlcChar(name, '0'));
                                break;
                            case "WChar":
                                sectionStruct.AddMember(new PlcWChar(name, '0'));
                                break;
                            case "String":
                                sectionStruct.AddMember(new PlcArray(name, new PlcChar(name, '0'), 256));
                                break;
                            case "WString":
                                //sectionStruct.AddMember(new PlcChar(name, '0'));
                                break;
                            case "IEC_TIMER":
                                break;
                            default:
                                sectionStruct.AddMember(new PlcArray(name, new PlcStruct(name, arrayInfo.ElementType), arrayInfo.Length));
                                break;
                        }


                    }
                    else if (dataType.StartsWith("String["))
                    {
                        int length = ArrayInfo.ExtractNumberFromBracket(dataType);
                        sectionStruct.AddMember(new PlcArray(name, new PlcChar(name, '0'), length));
                    }
                    else
                    {
                        sectionStruct.AddMember(new PlcStruct(name, dataType));
                        Debug.WriteLine($"不支持的数据类型: {dataType}，请检查数据类型定义。");
                    }
                    break;
            }
        }

        
        private void ParseFlgNetList(List<FlgNet_T> flgNet_list)
        {
            if(flgNet_list.Count>0)
            {
                foreach (var flgNetItem in flgNet_list)
                {
                    if (flgNetItem.Parts != null && flgNetItem.Parts.Length > 0)
                    {
                        ParseParts(flgNetItem.Parts);
                    }
                    //Debug.WriteLine(flgNetItem.ToString());
                    if(flgNetItem.Wires!=null&&flgNetItem.Wires.Wire.Length > 0)
                    {
                        //IdentCon 数据节点
                        //NameCon 指令对应的变量名
                        //OpenCon 根据博图查看为空节点用于连接NameCon
                        //Openbranch 推测为O,竖着的虚拟节点
                        //Powerrail 母线，能流的开始
                        foreach (var wire in flgNetItem.Wires.Wire)
                        {
                            Debug.WriteLine($"wire:{wire}");
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 解析所有节点
        /// </summary>
        /// <param name="parts"></param>
        private void ParseParts(Object[] parts)
        {
            foreach (var part in parts)
            {
                if(part is Part_T)
                {
                    var _part = (Part_T)part;
                    allCammand.Add(_part.Name);
                    ParsePart(_part);
                }
                else if(part is Call_T)
                {
                    var _call = (Call_T)part;
                    ParseCall(_call);
                }else if(part is Access_T)
                {
                    var _access = (Access_T)part;
                    ParseAccess(_access);
                }
            }
        }
        private void ParsePart(Part_T part_T)
        {
            if (part_T != null)
            {
                var uid = part_T.UId;
                var name = part_T.Name;
                Debug.WriteLine($"{uid},{name}");
                if(part_T.Negated!=null&&part_T.Negated.Length>0)
                {
                    foreach (var negatedItem in part_T.Negated)
                    {
                        Debug.WriteLine($"取反{negatedItem.Name}");
                    }
                }
            }
        }
        private void ParseCall(Call_T call_T)
        {
            ///call的调用需要绑定实例和参数
            ///FB需要绑定实例，如有参数则绑定参数
            ///FC只需要绑定参数
            if (call_T != null)
            {
                int uid = call_T.UId;
                string call_name= call_T.CallInfo.Name;
                string call_type = Enum.GetName(typeof(BlockType_TE), call_T.CallInfo.BlockType) ?? string.Empty;
                //只对FB做绑定实例解析
                if(call_T.CallInfo.BlockType==BlockType_TE.FB&&
                    call_T.CallInfo.Items!=null&&
                    call_T.CallInfo.Items.Length>0)
                {
                    foreach (var callInfoItem in call_T.CallInfo.Items)
                    {
                        //存在绑定变量，只解析实例
                        if (callInfoItem is Instance_T)
                        {
                            var _instance = (Instance_T)callInfoItem;
                            var instanceScope = Enum.GetName(typeof(Scope_TE), _instance.Scope) ?? string.Empty;
                            var instanceUId= _instance.UId;
                            if(_instance.Items!=null&&_instance.Items.Length>0)
                            {
                                foreach (var instanceItem in _instance.Items)
                                {
                                    if (instanceItem is Component_T)
                                    {
                                        var _component = (Component_T)instanceItem;
                                        var instance=ParseComponent(_component);
                                        Debug.WriteLine("实例名称：{0}，实例类型：{1}", instance, instanceScope);
                                    }
                                }
                            }   
                        }
                        
                    }
                }
                Debug.WriteLine($"Call UID: {uid}，call_name:{call_name},call_type:{call_type}");

            }
        }
        private string ParseAccess(Access_T access_T)
        {
            if (access_T != null)
            {
                var uid = access_T.UId;
                var scope = access_T.Scope;
                if (access_T.Item != null)
                {
                    if(access_T.Item is Constant_T)
                    {
                        var item = (Constant_T)access_T.Item;
                        return item.ConstantValue.Value;
                    }
                    else if(access_T.Item is Symbol_T)
                    {
                        var symbolItem = (Symbol_T)access_T.Item;
                        if(symbolItem.Items != null && symbolItem.Items.Length > 0)
                        {
                            string varname = string.Empty;
                            foreach (var symbolItemObj in symbolItem.Items)
                            {
                                if (symbolItemObj is Component_T)
                                {
                                    var _component = (Component_T)symbolItemObj;
                                    var componentName = ParseComponent(_component);
                                    varname+=componentName;
                                    varname += ".";
                                }
                            }
                            Debug.WriteLine($"varname{varname}");
                        }
                    }
                    
                }
            }
            return string.Empty;
        }   
        private string ParseComponent(Component_T component_T)
        {
            if (component_T != null)
            {
                AccessModifier_TE c_accessModifier = component_T.AccessModifier;
                var c_name = component_T.Name;
                //处理数组
                if(component_T.AccessModifier==AccessModifier_TE.Array)
                {
                    if(component_T.Items!=null&&component_T.Items.Length>0)
                    {
                        foreach (var compItem in component_T.Items)
                        {
                            if (compItem is Access_T)
                            {
                                var _access = (Access_T)compItem;
                                string index=ParseAccess(_access);
                                Debug.WriteLine($"[{index}]");
                                return $"{c_name}[{index}]";
                            }
                        }
                    }
                }
                //处理位
                //<Symbol>
                //  < Component Name = "源INT" SliceAccessModifier = "x11" />
                //</ Symbol >
                // .%x0
                else if (component_T.SliceAccessModifier!="undef")
                {
                    return $"{c_name}.%{component_T.SliceAccessModifier}";
                }
                else if (component_T.AccessModifier == AccessModifier_TE.None)
                {
                    return $"{c_name}";
                }
                  


            }
            return string.Empty;
        }
    }
}
